package dao.Impl;

import dao.BaseDao;
import dao.BookDao;
import entity.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangying
 * @date 2025/2/21 16:26
 * title
 */
public class BookDaoImpl extends BaseDao implements BookDao {


    @Override
    public List<Book> getBookBySortAndName(String name, int sort,int pageIndex,int pageSize) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Book> list = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "SELECT a.*,b.name from book a,book_sort b WHERE a.bookSort=b.id";
            List param = new ArrayList();
            if (name != null && !"".equals(name)) {
                sql = " and a.bookSort like ?  ";
                param.add("%" + name + "%");
            }
            if (sort != -1) {
                sql += " and a.bookSort = ? ";
                param.add(sort);
            }
            sql+=" limit ?,?";
            param.add(pageIndex);
            param.add(pageSize);
            ps = conn.prepareStatement(sql);
            if (param.size() > 0) {
                for (int i = 0; i < param.size(); i++) {
                    ps.setObject(i + 1, param.get(i));
                }
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setISBN(rs.getString("ISBN"));
                book.setBookName(rs.getString("bookName"));
                book.setBookAuthor(rs.getString("bookAuthor"));
                book.setBookPub(rs.getString("bookPub"));
                book.setBookSort(rs.getInt("bookSort"));
                book.setBookAbstract(rs.getString("bookAbstract"));
                book.setBookTotal(rs.getInt("bookTotal"));
                book.setBookPhoto(rs.getString("bookPhoto"));
                book.setBookPubLocation(rs.getString("bookPubLocation"));
                book.setBookPubYear(rs.getString("bookPubYear"));
                list.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, ps, conn);
        }
        return list;
    }

    @Override
    public int count(String name, int sort) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count=0;
        try {
            conn = getConnection();
            String sql = "SELECT count(1) from book a,book_sort b WHERE a.bookSort=b.id";
            List param = new ArrayList();
            if (name != null && !"".equals(name)) {
                sql = " and a.bookSort like ?  ";
                param.add("%" + name + "%");
            }
            if (sort != -1) {
                sql += " and a.bookSort = ? ";
                param.add(sort);
            }
            ps = conn.prepareStatement(sql);
            if (param.size() > 0) {
                for (int i = 0; i < param.size(); i++) {
                    ps.setObject(i + 1, param.get(i));
                }
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, ps, conn);
        }
        return count;
    }
}
